package edu.gyc.hishiro.shirio;

import edu.gyc.hishiro.model.Myuser;
import edu.gyc.hishiro.service.MyuserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthorizingRealm {

    @Autowired
    private MyuserService myuserService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("开始身份认证");
        String name=(String)token.getPrincipal();

        Myuser myuser=myuserService.lambdaQuery().eq(Myuser::getUsername,name).one();
        if (myuser == null) {
            return null;
        }

        SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(
           myuser.getUsername(),
                myuser.getPassword(),
                getName()
        );
        return simpleAuthenticationInfo;
    }
}
